package com.yybray;

import lombok.Data;

import java.nio.file.DirectoryStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * Hello world!
 */
@Data
public class Apple {

    int weight;
    String color;

    public Apple() {}
    public Apple(int weight, String color) {
        this.weight = weight;
        this.color = color;
    }
    public Apple(int weight) {
        this.weight = weight;
    }

    public static boolean isGreenApple(Apple apple) {
        return "green".equals(apple.getColor());
    }
    public static boolean isHeavyApple(Apple apple) {
        return apple.getWeight() > 100;
    }

    public static List<Apple> filterApple(List<Apple> list, Predicate<Apple> p) {
        List<Apple> apples = new ArrayList<>();
        for (Apple a : list) {
            if (p.test(a)) {
                apples.add(a);
            }
        }
        return apples;
    }

    public static void main(String[] args) {
        /*List<Apple> apples = Arrays.asList(
                new Apple(100, "green"),
                new Apple(150, "red")
        );*/
        List<Apple> apples = new ArrayList<Apple>() {
            {
                add(new Apple(100, "green"));
                add(new Apple(150, "red"));
                add(new Apple(){
                    {
                        setWeight(100);
                        setColor("yellow");
                    }
                });
            }
        };


        System.out.println(Apple.filterApple(apples, Apple::isGreenApple));
        System.out.println(Apple.filterApple(apples, Apple::isHeavyApple));
        System.out.println(Apple.filterApple(apples, a -> a.getWeight() > 100));
        System.out.println(apples.stream().filter((Apple a) -> a.getColor().equals("green")).collect(Collectors.toList()));
        System.out.println(apples.stream().filter(x -> x.getWeight() > 100).collect(Collectors.toList()));
    }

}
